package com.lfk.justweengine.utils.tools;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.location.Locator;
import ohos.net.NetManager;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;
import ohos.wifi.WifiDevice;

/**
 * 判断网络状态的工具类
 *
 * @author liufengkai
 */
public class NetUtils {

    private NetUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 判断网络是否连接
     *
     * @param context
     * @return 是否已连接网络
     */
    public static boolean isConnected(Context context) {

        NetManager cm =  NetManager.getInstance(context);
        return cm.hasDefaultNet();
    }

    /**
     * 判断是否是wifi连接
     *
     * @param context
     * @return 是否连接Wi-Fi
     */
    public static boolean isWifi(Context context) {
        // 先判断是否连接
        if (!isConnected(context)) {
            return false;
        }

        WifiDevice wifiDevice = WifiDevice.getInstance(context);
        if (wifiDevice == null) return false;

        return wifiDevice.isWifiActive();
    }


    /**
     * 打开网络设置界面
     *
     * @param ability
     */
    public static void openSetting(Ability ability) {
        Intent intent = new Intent();

        intent.setAction(IntentConstants.ACTION_WIRELESS_SETTINGS);
        intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        intent.setUri(Uri.getUriFromParts("package", ability.getBundleName(), null));
        ability.startAbilityForResult(intent, 0);
    }

    /**
     * 判断GPS是否打开
     *
     * @param context
     * @return
     */
    public static boolean isGpsEnabled(Context context) {

        Locator locator = new Locator(context);
        if (locator == null) return false;
        return locator.isLocationSwitchOn();
    }

}
